import Vue from 'vue'
import App from './App.vue'
import router from './router'
import Login from './Login'
import axios from "axios"
import bus from "./bus"
import store from "./store"
import './filters'
import './components'
// 请求拦截
axios.interceptors.request.use(config => {
  store.commit('changeIsLoading', true)
  config.url = "/api" + config.url + (config.url.includes('?') ? '&' : '?') + 't=' + Date.now()
  config.headers = {
    authorization: store.state.admin.token
  }
  return config
})
// 响应拦截
axios.interceptors.response.use(({ data }) => {
  store.commit('changeIsLoading', false)
  if (data.ok === -2) {
    store.commit('outAdminInfo');
  } else {
    if (data.pageSum) {
      store.commit('changePageSum', data.pageSum)
    }
  }
  return data;
})
Vue.directive(
  'red', (el, bind) => {
    el.style.background = bind.value
  })
Vue.prototype.$http = axios
Vue.config.productionTip = false
Vue.prototype.$bus = bus
new Vue({
  mounted() {
    this.$bus.$on('login', a => {
      this.isLogin = a;
    })
  },
  router,
  store,
  render: function (h) { return h(store.state.admin.adminName ? App : Login) }
}).$mount('#app')
